package com.itjeffrey.autocode.websocket;

import com.itjeffrey.autocode.util.ACBaseUtil;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * websocketserver容器，维护与客户端连接的websocketServerMap
 */
public class WebSocketServerContainer {

    /**
     * 与客户端连接的传输数据的websocketServer的集合
     */
    public static Map<String, CopyOnWriteArraySet<BaseSocketServer>> BASE_SOCKET_SERVER_MAP = new ConcurrentHashMap<>();

    /**
     * 根据客户端IP获取对应websocketServer集合
     *
     * @param ip
     * @return
     */
    public static synchronized CopyOnWriteArraySet<BaseSocketServer> getByIP(String ip) {
        return BASE_SOCKET_SERVER_MAP.get(ip);
    }

    /**
     * （无ip）首次初始化时调用
     *
     * @param ip
     * @param baseSocketServer 对象
     */
    public static synchronized void putBaseSocketServerMap(String ip, BaseSocketServer baseSocketServer) {
        CopyOnWriteArraySet<BaseSocketServer> set = new CopyOnWriteArraySet<>();
        set.add(baseSocketServer);
        BASE_SOCKET_SERVER_MAP.put(ip, set);
    }

    /**
     * 根据客户端IP添加对应的websocketServer
     *
     * @param ip
     * @param baseSocketServer 对象
     */
    public static synchronized void addSocketServerByIP(String ip, BaseSocketServer baseSocketServer) {
        //无ip则新增
        if (!BASE_SOCKET_SERVER_MAP.containsKey(ip)) {
            putBaseSocketServerMap(ip, baseSocketServer);
            return;
        }
        //有ip则追加
        if (getSocketServer(ip, ACBaseUtil.getObjClass(baseSocketServer)) != null) {
            removeSocketServer(ip, ACBaseUtil.getObjClass(baseSocketServer));
        }
        BASE_SOCKET_SERVER_MAP.get(ip).add(baseSocketServer);
    }

    /**
     * 根据客户端IP和WebSocketServer类名（非全限定类名）获取对应类型的SocketServer
     *
     * @param ip
     * @param socketServerType 如：WebSocketServer, TokenWebSocketServer
     * @return
     */
    public static synchronized BaseSocketServer getSocketServer(String ip, String socketServerType) {
        CopyOnWriteArraySet<BaseSocketServer> baseSocketServers = BASE_SOCKET_SERVER_MAP.get(ip);
        if (baseSocketServers == null) {
            return null;
        }
        for (BaseSocketServer baseSocketServer : baseSocketServers) {
            if (ACBaseUtil.getObjClass(baseSocketServer).equals(socketServerType)) {
                return baseSocketServer;
            }
        }
        return null;
    }

    /**
     * 根据客户端IP移除对应类别的SocketServer
     *
     * @param ip
     * @param socketServerType
     */
    public static synchronized void removeSocketServer(String ip, String socketServerType) {
        CopyOnWriteArraySet<BaseSocketServer> set = getByIP(ip);
        set.forEach(t -> {
            if (ACBaseUtil.getObjClass(t).equals(socketServerType)) {
                set.remove(t);
            }
        });
    }

    /**
     * 移除给定IP下所有的SocketServer
     *
     * @param ip
     */
    public static synchronized void removeBaseSocketServerMap(String ip) {
        BASE_SOCKET_SERVER_MAP.remove(ip);
    }
}
